package com.ruoyi.pos.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pos.domain.dto.shift.request.ShiftReportRequest;
import com.ruoyi.pos.domain.dto.shift.response.ShiftReportResponse;
import com.ruoyi.pos.service.shift.IPosShiftService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * POS交班管理Controller
 * 
 * @author ruoyi
 * @date 2025-01-30
 */
@Api(tags = "POS交班管理")
@RestController
@RequestMapping("/pos/shift")
public class PosShiftController extends BaseController {

    @Autowired
    private IPosShiftService posShiftService;

    /**
     * 生成班报
     */
    @ApiOperation("生成班报")
    @PreAuthorize("@ss.hasPermi('pos:shift:report')")
    @Log(title = "班报生成", businessType = BusinessType.EXPORT)
    @PostMapping("/generateShiftReport")
    public AjaxResult generateShiftReport(@RequestBody ShiftReportRequest request) {
        try {
            ShiftReportResponse response = posShiftService.generateShiftReport(request);
            return success(response);
        } catch (Exception e) {
            return error("班报生成失败：" + e.getMessage());
        }
    }

    /**
     * 生成日报
     */
    @ApiOperation("生成日报")
    @PreAuthorize("@ss.hasPermi('pos:shift:report')")
    @Log(title = "日报生成", businessType = BusinessType.EXPORT)
    @PostMapping("/generateDailyReport")
    public AjaxResult generateDailyReport(@RequestBody ShiftReportRequest request) {
        try {
            ShiftReportResponse response = posShiftService.generateDailyReport(request);
            return success(response);
        } catch (Exception e) {
            return error("日报生成失败：" + e.getMessage());
        }
    }

    /**
     * 获取班报数据（按时间范围）
     */
    @ApiOperation("获取班报数据")
    @PreAuthorize("@ss.hasPermi('pos:shift:query')")
    @GetMapping("/getShiftReport")
    public AjaxResult getShiftReport(@RequestParam String cashierCode,
                                   @RequestParam String deviceCode,
                                   @RequestParam String startTime,
                                   @RequestParam String endTime) {
        try {
            ShiftReportRequest request = new ShiftReportRequest();
            request.setCashierCode(cashierCode);
            request.setDeviceCode(deviceCode);
            request.setReportType("SHIFT");
            // 设置时间范围
            // request.setShiftStartTime(parseDate(startTime));
            // request.setShiftEndTime(parseDate(endTime));
            
            ShiftReportResponse response = posShiftService.generateShiftReport(request);
            return success(response);
        } catch (Exception e) {
            return error("获取班报数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取日报数据（按日期）
     */
    @ApiOperation("获取日报数据")
    @PreAuthorize("@ss.hasPermi('pos:shift:query')")
    @GetMapping("/getDailyReport")
    public AjaxResult getDailyReport(@RequestParam String cashierCode,
                                   @RequestParam String deviceCode,
                                   @RequestParam String reportDate) {
        try {
            ShiftReportRequest request = new ShiftReportRequest();
            request.setCashierCode(cashierCode);
            request.setDeviceCode(deviceCode);
            request.setReportType("DAILY");
            // 设置日期
            // request.setReportDate(parseDate(reportDate));
            
            ShiftReportResponse response = posShiftService.generateDailyReport(request);
            return success(response);
        } catch (Exception e) {
            return error("获取日报数据失败：" + e.getMessage());
        }
    }
}
